package com.flyme.common.core.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;


/**
 * @Author:yexg
 * @Date:2020-06-12 11:11
 * @Description:
 **/
public class STSOSSTokenUtils {
    public static String accessKeyId = "LTAITwwT7b13wJ6u";
    public static String accessKeySecret = "9aBCPmQZ28DFDUQRu7TG1KmRihFwVw";
    public static String roleArn = "acs:ram::1654531120201911:role/qinbojiaoyhu"; // 即角色详情页的Arn值
    // RoleSessionName 是临时Token的会话名称，自己指定用于标识你的用户，主要用于审计，或者用于区分Token颁发给谁
    // 但是注意RoleSessionName的长度和规则，不要有空格，只能有'-' '_' 字母和数字等字符
    // 具体规则请参考API文档中的格式要求
    public static String roleSessionName = "roleSessionName"; // 自定义即可

    public static AssumeRoleResponse getOssAccessKeySecret() throws ClientException {
        DefaultProfile.addEndpoint("", "Sts", "sts.aliyuncs.com");
        // 构造default profile（参数留空，无需添加region ID）
        IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
        // 用profile构造client
        DefaultAcsClient client = new DefaultAcsClient(profile);
        final AssumeRoleRequest request = new AssumeRoleRequest();
        request.setSysMethod(MethodType.POST);
        request.setRoleArn(roleArn);
        request.setRoleSessionName(roleSessionName);
        final AssumeRoleResponse response = client.getAcsResponse(request);
        return response;
    }



}
